<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/src/css/layui.css}">
</head>
<body>
<blockquote class="layui-elem-quote list_search">
    <div class="layui-inline">
        <a class="layui-btn layui-btn-normal adminAdd_btn"><i
                class="layui-icon">&#xe608;</i> 添加管理员</a>
    </div>
    <div class="layui-inline">
        <a class="layui-btn layui-btn-danger batchDel"><i
                class="layui-icon">&#xe640;</i>批量删除</a>
    </div>
</blockquote>
<!--管理员Id-->
<input type="hidden" value="18" id="adminId">
<table id="adminList" lay-filter="test"></table>


<script th:src="@{/src/layui.js}"></script>
    <script>
        layui.use(['table','jquery','layer','form'],function () {
            var table = layui.table,
            $ = layui.jquery,
            layer = layui.layer;
                table.render({
                id:'adminList',
                elem:'#adminList',
                cellMinWidth:80,
                url:'/account/list', //请求加载数据的接口
                page:true,
                limit:10,
                limits:[10,20,30],
                toolbar:true,
                cols:[
                    [
                        {type:'checkbox'},
                        {title:'ID',field:'id'},
                        {title:'登录账号',field:'account'},
                        {title:'邮箱',field:'email'},
                        {title:'性别',field:'sex',templet:'#sexTml'},
                        {title:'qq',field:'qq'},
                        {title:'联系方式',field:'phone'},
                        {title:'角色',field:'role_name'},
                        {title:'操作',field:'operate',templet:'#barEdit'}
                    ]
                ],
                parseData:function (result) {//result是服务器端响应的数据
                    return {
                        code:result.code ==200?0:result.code(),
                        msg:result.message,
                        data:result.data.list,
                        count:result.data.total
                    }
                }
            });
            //添加管理员
            $(".adminAdd_btn").click(function () {
                var index = layer.open({
                    type:2,
                    title:'编辑管理员',
                    content:'/admin/addAdmin'
                })
                //窗口大小改变的事件
                $(window).resize(function () {
                    layer.full(index);
                })
                layer.full(index);
            })
            //批量删除
            $(".batchDel").click(function () {
                //获取选中的行
                var checkStatus = table.checkStatus('adminList');//table中定义的id
                var data = checkStatus.data;
                var adminId = $("#adminId").val();
                if(data.length == 0){
                    layer.msg('请选择数据',{icon:5});
                    return ;
                }
                var flag = false;
                var keys = "";
                $.each(data,function (index,obj) { //jquery中的foreach函数
                    if(obj.roleName == "超级管理员"){
                        flag = true;
                        layer.msg('超级管理员不能删除');
                        return;
                    }
                    if(obj.id == adminId){
                        flag = true;
                        layer.msg('不能删除自己');
                        return ;
                    }
                    keys += obj.id+",";
                })
                if(flag){
                    return ;
                }
                keys = keys.substring(0,keys.length-1);
                layer.confirm('确认删除？',function (index) {
                    $.ajax({
                        url:'/admin/delAdmin/'+keys,
                        type:'delete',
                        dataType:'json',
                        success:function (result) {
                            if(result.code == 200){
                                layer.msg('删除成功',{icon:1},function () {
                                    location.reload();//刷新数据
                                });
                            }else{
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);//关闭当前提示框
                })

            });

            //监听行工具事件
            //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            //obj.data //获得当前行数据
            //var layEvent = obj.event; //获得 lay-event 对应的值
            table.on('tool(test)',function (obj) {
                var event = obj.event;
                var data = obj.data;
                if(event === 'edit'){ //执行编辑
                    layer.open({
                        type:2,
                        title:'编辑管理员',
                        area:['450px','560px'],
                        content:'/admin/editAdmin/'+data.id
                    });
                }else if(event === 'del'){//执行删除
                    var adminId = $("#adminId").val();
                    if(data.roleName == "超级管理员"){
                        layer.msg('超级管理员不能删除');
                        return ;
                    }
                    if(data.id == adminId){
                        layer.msg('不能删除自己');
                        return ;
                    }

                    layer.confirm('确认删除',function (index) {

                        $.ajax({
                            url:'/admin/deleteAdmin/'+data.id,
                            type:'delete',
                            dataType:'json',
                            success:function (result) {
                                if(result.code == 200){
                                    layer.msg("删除成功",{icon:1});
                                    location.reload();
                                }else{
                                    layer.msg("删除失败",{icon:5});
                                }
                            }
                        })

                        layer.close(index);
                    })
                }
            })


        });

    </script>

    <script type="text/html" id="barEdit">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <script type="text/html" id="sexTml">
            {{# if(d.sex==1){ }}
                <span>男</span>
            {{# }else{ }}
                <span>女</span>
            {{# } }}
    </script>


<!--<hr>-->
    <!--iframe嵌入一个子窗口-->
<!--<iframe src="http://www.baidu.com" width="500px" height="500px" frameborder="0"></iframe>-->











</body>
</html>